package com.redstar.HappyRefresh.manage;

import com.redstar.HappyRefresh.authority.Authority;
import com.redstar.HappyRefresh.cache.UserInfo;
import com.redstar.HappyRefresh.pojo.Permission;
import com.redstar.HappyRefresh.pojo.Result;
import com.redstar.HappyRefresh.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/authorize/permission")
public class PermissionManage {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/list")
    @Authority("permission:list")
    public List<Permission> list() { // 查询所有的权限
        return permissionService.list();
    }

    @GetMapping("/treeSelect")
    @Authority("permission:treeSelect")
    public List<Permission> treeSelect() { // 权限管理，构造只包含目录和菜单的权限树
        return permissionService.treeSelect();
    }

    @PostMapping
    @Authority("permission:add")
    public Result add(@RequestBody Permission permission) { // 添加权限
        permission.setIcon("fa " + permission.getIcon());
        permissionService.save(permission);
        return Result.succeed();
    }

    @PutMapping
    @Authority("permission:update")
    public Result update(@RequestBody Permission permission) { // 修改权限
        permission.setIcon("fa " + permission.getIcon());
        permissionService.updateById(permission);
        return Result.succeed();

    }

    @DeleteMapping("/{id}")
    @Authority("permission:delete")
    public Result delete(@PathVariable Long id) { // 删除权限
        permissionService.removeMenu(id);
        return Result.succeed().message("删除成功");
    }

    @GetMapping("/initMenu")
    public Map<String, Object> initMenu() { // 初始化菜单
        return permissionService.initMenu(UserInfo.get());
    }
}

